<!--

/*
* Copyright (C) 2005-2013 University of Sydney
*
* Licensed under the GNU License, Version 3.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/**
* simple HAPI app that exports CVS records from a query.
*
* @author      Stephen White   <stephen.white@sydney.edu.au>
* @author      Artem Osmakov   <artem.osmakov@sydney.edu.au>
* @copyright   (C) 2005-2013 University of Sydney
* @link        http://Sydney.edu.au/Heurist
* @version     3.1.0
* @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
* @package     Heurist academic knowledge management system
* @subpackage  Export
*/

-->

<html>
	<head>
		<title>Export Records to CSV (comma-separated values)</title>

		<!--
		This HAPI tool is designed to export multiple details for a selected resource
		type. For REFERENCE it is designed to export a key detail for the refernced record
		type. It is written generically to allow any HRecordType and HDetailType
		REFERENCE between any 2 HRecords.  The tool reads the record types from Heurist
		and requires the user to select the HDetailTypes to export and if any of the
		HDetailTypes are REFERENCE the user is requested to select the output HDetail for
		the REFERENCE object. The user is allowed to select the delimiters for the output
		data which is output to a textarea for cut and paste. Multivalued fields are delimited
		with a vertical bar (|).
		-->

		<script src="../../common/php/loadHAPI.php"></script>
		<script src="../../common/php/getMagicNumbers.php"></script>
		<script>
			if (!(HCurrentUser.isLoggedIn()&& HCurrentUser.isAdministrator())) {
				//	   window.location =  HAPI.HeuristBaseURL + "common/connect/login.php"; // saw TODO: add instance code here
			}
		</script>

		<script src="exportDelimitedForRectype.js"></script>

		<link href="../../common/css/global.css" type="text/css" rel="stylesheet">
		<link href="../../common/css/edit.css" type="text/css" rel="stylesheet">

		<style>
			#csv-textarea {
				width: 100%;
				height: 400px;
				border: 2px solid black;
			}
			#csv-separator, #csv-terminator { width: 30px; }
			table { border-collapse: collapse; }
			td { width: 100px; border: 1px solid gray; padding: 2px; }
			td select { width: 100px; }
			td.error { color: red; }
			#col-select-row td { border: none; padding: 1px 5px 5px 0px; vertical-align: top; }

			.input-header-cell{
				width:300px !important;
			}
			select {
				width:200px;
			}
		</style>

	</head>

	<body class="popup" onload = "getRecTypes();">
		<!-- <h2>Delimited data export</h2 -->
		<p>
			This function exports data for specified fields of a selected record type in comma or tab separated format.<br />
			An additional query string using normal Heurist query structure can be used to select a subset of the record type.<br />
			Due to the limitations of the delimited text format, only one record type can be exported at a time.
		</p>

		<div style="padding-top:10px;width:600px">

			<div class="input-row">
				<label class="input-header-cell">Select record type</label>
				<div class="input-cell" id="select-rec-type"></div>
			</div>
			<div class="input-row">
				<label class="input-header-cell">Select fields to export</label>
				<div class="input-cell" id="select-detail-type"><select size="4"></select></div>
			</div>
			<div class="input-row">
				<label class="input-header-cell">Include internal field code in column preceding values</label>
				<div class="input-cell" style="padding-top: 4px;"><input type="checkbox" id="dtIDsCheckbox" onchange="updateExportMap()" />  &nbsp;&nbsp;(used by Heurist 'field updater')</div>
			</div>
			<div class="input-row">
				<label class="input-header-cell">Heurist query string (optional) - provides additional filtering)</label>
				<div class="input-cell"><input type="text"  style="width:280px;" id="queryInput" onkeydown="clearOutput()"/></div>
			</div>
			<div class="input-row">
				<label class="input-header-cell">Use delimeter</label>
				<div class="input-cell"><select style="width:80px" id="delimiterSelect" onchange="refreshRecordData()"><option value=",">comma</option><option value="\t">tab</option></select></div>
			</div>
			<!--
			<div class="input-row">
				<label class="input-header-cell">Quote</label>
			 	<div class="input-cell"><select id="quoteSelect" onchange="refreshRecordData()"><option selected value='0'>&nbsp;</option><option value='"'>"</option><option value="'">'</option></select><br/><br/>
			</div>
			-->
			<div class="input-row">
				<label class="input-header-cell">Include field names as first output row</label>
				<div class="input-cell" style="padding-top: 4px;"><input type="checkbox" id="includeFieldNamesCheckbox" onchange="refreshRecordData()"/></div>
			</div>
			<div class="input-row" style="text-align: center;">

				<input type="button" onclick="getRecords()" value="generate delimited data"/>
			</div>

		</div>

		<div id="export-detail-map" style="padding-top:10px;"></div>

		<div id="results"></div>

		<p id=records-p></p>
	</body>
</html>
